﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SCCN.BLL;
using SCCN.DAL;
using SCCN.Utility;
using System.Text;

/// <summary>
///CreateHeader 的摘要说明
/// </summary>
public class CreateArticle : CreatePage
{
    public static int articleId = 0;
    public static string articleTitle = string.Empty;
    public static string articleContent = string.Empty;
    public static string articleTag = string.Empty;
    public static string publishDate = string.Empty;
    public static string categoryName = string.Empty;
    public static string categoryId = string.Empty;

    public CreateArticle()
    {
    }

    /// <summary>
    /// 生成
    /// </summary>
    /// <returns></returns>
    public static bool Create()
    {
        string path = ReaderTemplate("~/template/details.html");
        string content = Fun_Rapalce_Tag(path);
        try
        {
            CreateHTMLHelper.CreateFile(HttpContext.Current.Server.MapPath("~/article/" + articleId + ".shtml"), CreatePage.encode, content);
            ArticleBLL bllArticle = ArticleBLL.getInstance();
            SC_Article obj = bllArticle.GetSC_ArticleById(articleId);
            obj.CreateUrl = "/article/" + articleId + ".shtml";
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return true;
    }

    /// <summary>
    /// 替换页面标签
    /// </summary>
    /// <param name="content"></param>
    /// <returns></returns>
    public static string Fun_Rapalce_Tag(string content)
    {
        //标签存放字典
        Dictionary<string, string> dict = new Dictionary<string, string>();
        Article_Info();
        dict.Add("{$Article_Title$}", articleTitle);//头部分类
        dict.Add("{$Article_PublishDate$}", publishDate);//头部分类
        dict.Add("{$Article_Tag$}", articleTag);//头部分类
        dict.Add("{$Article_Content$}", HttpUtility.HtmlDecode(articleContent));//头部分类
        dict.Add("{$Article_Banner$}", Article_Banner());//头部分类
        dict.Add("{$Article_Category$}", categoryName);//头部分类
        dict.Add("{$categoryId$}", categoryId);
        dict.Add("{$Article_Relation$}", Article_Relation());
        content = CreateHTMLHelper.ReplaceTag(dict, content);
        return content;
    }


    /// <summary>
    /// 资讯信息
    /// </summary>
    /// <returns></returns>
    public static string Article_Info()
    {
        StringBuilder str = new StringBuilder();
        ArticleBLL bllArticle = ArticleBLL.getInstance();
        CategoryBLL bllCategory = CategoryBLL.getInstance();
        SC_Article obj = bllArticle.GetSC_ArticleById(articleId);
        articleTitle = obj.ArticleTitle;
        articleContent = obj.ArticleContent;
        articleTag = obj.Tag;
        publishDate = obj.CreateDate.ToString();
        categoryName = bllCategory.GetCategoryByID(Convert.ToInt32(obj.CategoryId)).CategoryName;
        categoryId = obj.CategoryId.ToString();
        return str.ToString();
    }

    /// <summary>
    /// 资讯头部banner
    /// </summary>
    /// <returns></returns>
    public static string Article_Banner()
    {
        AdSpaceBLL bllAdSpace = AdSpaceBLL.getInstance();
        AdvertisementBLL bllAdvertisement = AdvertisementBLL.getInstance();
        SC_AdSpace objAdSpace = bllAdSpace.GetAdSpaceByName("列表页头部广告位");
        List<SC_Advertisement> list = bllAdvertisement.GetAdvertisementByAdSpaceId(objAdSpace.Id);
        StringBuilder str = new StringBuilder();
        str.Append("<ul class=\"slide_nav\">");
        int i = 1;
        foreach (SC_Advertisement var in list)
        {
            if (i == 1)
            {
                str.Append("<li><a href=\"" + var.TargetUrl + "\" class=\"current\">0" + i + "</a></li>");
            }
            else
            {
                str.Append("<li><a href=\"" + var.TargetUrl + "\">0" + i + "</a></li>");
            }
            i++;
        }
        str.Append("</ul><div class=\"slide_box\">");
        i = 0;
        foreach (SC_Advertisement var in list)
        {
            str.Append("<a href=\"#\"><img src=\"" + var.PicSrc + "\" alt=\"\" /></a>");
        }
        str.Append("</div>");
        return str.ToString();
    }

    /// <summary>
    /// 读取相关文章信息
    /// </summary>
    /// <returns></returns>
    public static string Article_Relation()
    {
        StringBuilder str = new StringBuilder();
        ArticleBLL bllArticle = ArticleBLL.getInstance();
        CategoryBLL bllCategory = CategoryBLL.getInstance();
        SC_Article obj = bllArticle.GetSC_ArticleById(articleId);
        obj.IsDelete = false;
        List<SC_Article> list = bllArticle.GetArticleByCategoryId(Convert.ToInt32(obj.CategoryId));
        str.Append("<div class=\"art-other\">");
        str.Append("<div class=\"art-other-bt\">");
        str.Append("</div>");
        str.Append("<div class=\"art-other-body\">");
        str.Append("<ul class=\"ll mt10 w110p\">");
        int i = 0;
        foreach (SC_Article var in list)
        {
            if (i < 3)
            {
                str.Append("<li>");
                str.Append("<div>");
                str.Append("<a href=\"" + var.CreateUrl + "\">");
                str.Append("<img src=\"" + var.PicCoverUrl2 + "\" alt=\"\" /></a></div>");
                str.Append("<p class=\"toe\">");
                str.Append("<a href=\"" + var.CreateUrl + "\">" + var.ArticleTitle + "</a></p>");
                str.Append("</li>");
            }
            i++;
        }
        str.Append("</ul>");
        str.Append("</div></div>");
        return str.ToString();
    }

}